package com.chenliang.dao;

import com.chenliang.domain.Role;
import com.chenliang.domain.UserInfo;
import org.apache.ibatis.annotations.*;
import org.apache.ibatis.mapping.FetchType;

import java.util.List;

/**
 * @ProjectName: mavenSSM
 * @Package: com.chenliang.dao
 * 作用：
 */
public interface IUsersInfo {
    @Select("Select * from USERINFO")
    List<UserInfo> findall();

    @Select("Select * from USERINFO where username=#{username}")
    @Results(value = {
            @Result(id = true,property = "id",column = "id"),
            @Result(property = "roles",column = "id",javaType =java.util.List.class ,many = @Many(select = "com.chenliang.dao.IRoledao.findByuserInfoid",fetchType = FetchType.LAZY))
    })
    UserInfo findByUsername(String username);






    @Insert("INSERT INTO USERINFO (ID, EMAIL, USERNAME, PASSWORD, PHONENUM, STATUS) VALUES (SYS_GUID(), #{email}, #{username}, #{password},#{phoneNum}, #{status})")
    void addUser(UserInfo users);


//    用户详情
    @Select("select * from userInfo where id=#{id}")
    @Results(value = {
            @Result(id = true,property = "id",column = "id"),
            @Result(property = "roles",column = "id",javaType = java.util.List.class,
            many = @Many(select = "com.chenliang.dao.IRoledao.findByuserInfoid",fetchType = FetchType.LAZY))
    })
    UserInfo findById(String id);


    @Select("select * from Role where id not in(select roleId from userInfo_role where userId=#{id})")
    List<Role> findByIdWithoutRole(String id);

    @Select("select * from userInfo where id in(select userId from userInfo_role where roleId=#{id})")
    List<UserInfo> findByRoleId(String Id);

//    用户添加权限
    @Insert("insert into userInfo_role values(#{userId},#{roleId})")
    void addRole(@Param("userId") String userId, @Param("roleId")String id);
}


